// Copyright (c) 2025 Shudou.CN

frappe.query_reports["Trial Balance Simple CN"] = {
    filters: [
        {
            fieldname: "company",
            label: __("公司"),
            fieldtype: "Link",
            options: "Company",
            default: "",
//            default: frappe.defaults.get_user_default("Company"),
        },
        {
            fieldname: "fiscal_year",
            label: __("会计年度"),
            fieldtype: "Link",
            options: "Fiscal Year",
            default: "",
//            default: erpnext.utils.get_fiscal_year(frappe.datetime.get_today()),
        },
        {
            fieldname: "periodicity",
            label: __("显示：期间"),
            fieldtype: "Check",
            default: 1,
            onchange: function() {
                const periodicity = frappe.query_report_filters_by_name.periodicity.get_value();
                const periodicity_type = frappe.query_report_filters_by_name.periodicity_type;
                const report = frappe.query_report;

                // 显示/隐藏期间类型选择框
                periodicity_type.df.hidden = !periodicity;
                periodicity_type.refresh();

                // 刷新报表以更新列显示（关键：传递periodicity状态）
                report.refresh();
            }
        },
        {
            fieldname: "periodicity_type",
            label: __("会计分期"),
            fieldtype: "Select",
            options: [
                "Monthly",
                "Quarterly",
                "Yearly"
            ],
            default: "Monthly",
            hidden: 0
        },
        {
            fieldname: "cost_center",
            label: __("成本中心"),
            fieldtype: "MultiSelectList",
            options: "Cost Center",
            get_data: function(txt) {
                return frappe.db.get_link_options("Cost Center", txt);
            }
        },
        {
            fieldname: "project",
            label: __("项目"),
            fieldtype: "MultiSelectList",
            options: "Project",
            get_data: function(txt) {
                return frappe.db.get_link_options("Project", txt);
            }
        },
        {
            fieldname: "voucher_type",
            label: __("凭证类型"),
            fieldtype: "MultiSelectList",
            get_data: function(txt) {
                return frappe.db.get_list("DocType", {
                    fields: ["name"],
                    filters: {
                        "istable": 0,
                        "issingle": 0,
                        "name": ["in", ["Payment Entry", "Sales Invoice", "Purchase Invoice", "Journal Entry"]]
                    },
                    order_by: "name"
                }).then(results => {
                    return results.map(d => d.name);
                });
            }
        },
        {
            fieldname: "voucher_series",
            label: __("凭证字"),
            fieldtype: "MultiSelectList",
            get_data: function(txt) {
                return frappe.call({
                    method: "accounting_cn.api.get_voucher_series",
                    freeze: true,
                    freeze_message: __("加载凭证字...")
                }).then(r => {
                    if (r.message) {
                        // 过滤并返回匹配的前缀
                        return r.message.filter(series => 
                            series.toLowerCase().includes(txt.toLowerCase())
                        );
                    }
                    return [];
                });
            }
        },
        {
            fieldname: "show_company",
            label: __("显示：公司"),
            fieldtype: "Check",
            default: 1
        },
        {
            fieldname: "remove_company_abbreviation",
            label: __("移除公司简称"),
            fieldtype: "Check",
            default: 0,
            description: __("勾选后将自动去除成本中心和账户字段中的公司简称（如“- N”）")
        },
        {
            fieldname: "show_cost_center",
            label: __("显示：成本中心"),
            fieldtype: "Check",
            default: 1
        },
        {
            fieldname: "show_project",
            label: __("显示：项目"),
            fieldtype: "Check",
            default: 0
        }
    ],

    onload: function(report) {
        // 加载时的初始化逻辑
        report.page.add_inner_button(__("Export as Excel"), function() {
            report.export_report("Excel");
        });
    }
};